package mmc.ui;

import static org.lwjgl.opengl.GL11.GL_LIGHTING;
import static org.lwjgl.opengl.GL11.GL_MODELVIEW;
import static org.lwjgl.opengl.GL11.GL_PROJECTION;
import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D;
import static org.lwjgl.opengl.GL11.glColor3f;
import static org.lwjgl.opengl.GL11.glDisable;
import static org.lwjgl.opengl.GL11.glEnable;
import static org.lwjgl.opengl.GL11.glLoadIdentity;
import static org.lwjgl.opengl.GL11.glMatrixMode;
import static org.lwjgl.opengl.GL11.glOrtho;
import static org.lwjgl.opengl.GL11.glPopMatrix;
import static org.lwjgl.opengl.GL11.glPushMatrix;

import java.awt.Canvas;
import java.util.ArrayList;
import java.util.List;

import org.lwjgl.opengl.GL11;

import mmc.Client;
import static mmc.resource.Text12p.*;

public class UI {
	private Canvas canvas;
	private Client client;
	private boolean visible = true;
	public boolean isAnythingOpen = false;
	public Window openWindow;
	
	public static List<Window> windows = new ArrayList<Window>();

	//components
	public Chat chat;
	private Text text;
	private Radar radar;
	private UnitFrame unitframe;
	public Bartender bar;
	public Castbar cast;
	private Buffbar buffbar;
	
	public UI(Canvas canvas, Client client)
	{
		this.canvas = canvas;
		this.client = client;
		init();
	}
	
	public void init()
	{
		bar = new Bartender(0.25f, 0.95f, 0.5f, 0.05f);
		radar = new Radar(.8f, 0f, .2f, .2f, client);
		unitframe = new UnitFrame(0.05f, 0.05f, 0.2f, 0.05f, client);		
		chat = new Chat(0.0f, 0.7f, 0.4f, 0.2f, client);
		text = new Text(0.3f, 0.2f, 0.3f, 0.6f);
		cast = new Castbar(0.4f, 0.85f, 0.2f, 0.05f);
		buffbar = new Buffbar(0.6f, 0.0f, 0.2f, 0.1f);
		
		windows.add(new Menu(0.35f, 0.25f, 0.3f, 0.5f));
		windows.add(new KeyBindingWindow(0.25f, 0.2f, 0.5f, 0.6f));
		windows.add(new SkillWindow(0.25f, 0.2f, 0.5f, 0.6f, bar));
		windows.add(new Logout(0.4f, 0.4f, 0.2f, 0.2f));
	}
	
	public void render()
	{	
		//disable texturing and lighting
		glDisable(GL_LIGHTING);
		glDisable(GL_TEXTURE_2D);
		
		//setup 2D
		glMatrixMode(GL_PROJECTION);
		glPushMatrix();
		glLoadIdentity();				
		glOrtho(0, (int)canvas.getSize().getWidth(), (int)canvas.getSize().getHeight(), 0, -1, 1);
		glMatrixMode(GL_MODELVIEW);
		glPushMatrix();
		glLoadIdentity();
		GL11.glScalef((int)canvas.getSize().getWidth(), (int)canvas.getSize().getHeight(), 0);
		
		//render all ui components		
		if(visible)
		{
			radar.render();
			buffbar.render();
			unitframe.render();
			bar.render();
			chat.render();
			text.render();
			cast.render();
			
			
			for (Window win : windows) {
				win.render();
			}
		}
		glColor3f(1,1,1);
		try{
			glText12p(Client.client.getFPS().toString()+", ping="+(Client.client.getScene().getPlayer().getRemotePlayer().currentPing*1e3+" ms"), 0, 0);
			glText12p(
					"Net: Down: "+Client.client.getScene().getUpdatesPerSecond()+", "+
					Client.client.getScene().getPacketsPerSecond()+", "+
					String.format("%.2f",client.getScene().getContext().getByteDownCounter().getAps()/1024)+" KB/s, Up: "+
					Client.client.getScene().getContext().getPacketsOut()+", "+
					String.format("%.2f",client.getScene().getContext().getByteUpCounter().getAps()/1024)+" KB/s", 
					0, (float)(10/canvas.getSize().getHeight()));
		}catch(NullPointerException e){}
		
		//restore 3D
		glPopMatrix();
		glMatrixMode(GL_PROJECTION);
		glPopMatrix();
		glMatrixMode(GL_MODELVIEW);
		
		//restore texturing and lighting
		glEnable(GL_TEXTURE_2D);
		glEnable(GL_LIGHTING);
	}
	
	public void safeChangesOnClose(){
		for (Window win : windows) {
			win.safeChangesOnShutdown();
		}
		bar.safeChangesOnShutdown();
	}
	
	public void changeVisibility()
	{
		this.visible = !visible;
	}
	
	public void openWindow(Window com)
	{
		if(this.isAnythingOpen)
		{
			this.closeWindow();
		}
		this.isAnythingOpen = true;
		this.openWindow = com;
		this.openWindow.openWindow();
	}	
	
	public void closeWindow()
	{
		this.isAnythingOpen = false;
		this.openWindow.closeWindow();
		this.openWindow = null;
	}
}
